#include "stock.h"
#include "ansi_color_codes.h"
#include <iostream>

using namespace std;

stock::stock (const std::string& n, double _price, double _volatility, int _n_times, int _m_secs) : name(n), volatility (_volatility), dice (-volatility, volatility) {
  
  initial = price = _price;
  change = 0;

  n_times = _n_times;
  m_secs = _m_secs;
  update = m_secs * 1. / n_times;
   
}

int stock::tick (double now) { // try to move price of stock 
  
  double elapsed = now - last_change; // elapsed time since last price change
  
  
  if (elapsed > update) { // can update
    
    double last = price;
    
    price += dice (); // randomly move price
    
    if (price <= 0) price = last;
    
    change = price - last; // price change
    total_change = price - initial; // price change from start of program
    
    string color;
    if (change > 0) color = HIGH; else color = LOW;
    
    last_change = now; // note time now to change price only after elapsed time

    // print
    cout << color << "symbol: " << name << " price: " << price << " change: " << change << " total: " << total_change << " time: " << now << endl;
    
    return 1;
    
  } 
  
  return 0;

  
}
